package cn.itcast.dao.impl;

import cn.itcast.dao.DinnerTableDao;
import cn.itcast.entity.DinnerTable;
import cn.itcast.utils.JdbcUtil;
import java.util.List;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class DinnerTableDaoImpl implements DinnerTableDao {
	public void add(DinnerTable dt) {
		String sql = "INSERT INTO dinnerTable(tableName) VALUES (?);";
		try {
			JdbcUtil.getQueryRunner().update(sql, dt.getTableName());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void delete(int id) {
		String sql = "delete from dinnerTable where id=?";
		try {
			JdbcUtil.getQueryRunner().update(sql, Integer.valueOf(id));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public List<DinnerTable> search(String keywords) {
		String sql = "SELECT * FROM dinnerTable WHERE tableName LIKE ?";
		try {
			return (List) JdbcUtil.getQueryRunner().query(sql,
					new BeanListHandler(DinnerTable.class),
					new Object[] { "%" + keywords + "%" });
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public DinnerTable findById(int id) {
		String sql = "SELECT * FROM dinnerTable WHERE id=?";
		try {
			return (DinnerTable) JdbcUtil.getQueryRunner().query(sql,
					new BeanHandler(DinnerTable.class),
					new Object[] { Integer.valueOf(id) });
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void update(DinnerTable dt) {
		String sql = "update dinnerTable set tableStatus=?, orderDate=? where id=?";
		try {
			JdbcUtil.getQueryRunner().update(
					sql,
					new Object[] { Integer.valueOf(dt.getTableStatus()),
							dt.getOrderDate(), Integer.valueOf(dt.getId()) });
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}